package com.example.sea.back.web.user;

import com.alibaba.druid.util.StringUtils;
import com.example.sea.core.data.user.AuthPermissionEnum;
import com.example.sea.core.data.user.entity.AuthPermission;
import com.example.sea.core.data.user.model.AuthPermissionModel;
import com.example.sea.core.data.user.service.AuthPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/7/9 下午 3:09
 */
@Controller
@RequestMapping("/user/permission")
public class UserPermissionController {

    @Autowired
    private AuthPermissionService authPermissionService;

    /**
     * List页面
     * @param request
     * @param pageNum
     * @param pageSize
     * @param state
     * @param name
     * @return
     */
    @GetMapping("/index")
    public String indexHtml(HttpServletRequest request,Integer pageNum, Integer pageSize, Integer state, String name){
        Map params=new HashMap();
        params.put("pageNum",pageNum==null?1:pageNum);
        params.put("pageSize",pageSize==null?10:pageSize);
        params.put("state",state);
        params.put("name",StringUtils.isEmpty(name)?null:"%"+name+"%");
        params.put("parentId","0");
        params.put("type",AuthPermissionEnum.TYPE_BACK_PERMISSION.getValue());
        PageInfo<AuthPermission> pageInfo = authPermissionService.findPageByParams(params);
        request.setAttribute("pageInfo",pageInfo);
        /**
         * 回显数据
         */
        request.setAttribute("state",state);
        request.setAttribute("name",name);
        return "user/permission/index";
    }

    /**
     * 跳转添加
     * @return
     */
    @GetMapping("/add")
    public String addHtml(){
        return "user/permission/add";
    }

    /**
     * 添加
     * @param authPermission
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public Map addSubmit(AuthPermission authPermission){
        Map resultMap=new HashMap();
        int add = authPermissionService.add(authPermission);
        if (add==1){
            resultMap.put("code",200);
        }else{
            resultMap.put("code",500);
        }
        return resultMap;
    }

    /**
     * 跳转修改
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/update")
    public String updateHtml(HttpServletRequest request,String id){
        AuthPermission authPermission = authPermissionService.findById(id);
        request.setAttribute("authPermission",authPermission);
        if (authPermission.getParentId().equals(AuthPermissionEnum.PERMISSION_PARENT_ID.getValue())){
            return "user/permission/edit";
        }else{
            return "user/permission/edit-menu";
        }
    }

    /**
     * 修改数据
     * @param authPermission
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public Map updateSubmit(AuthPermission authPermission){

        AuthPermission oldEntity = authPermissionService.findById(authPermission.getId());
        oldEntity.setCode(authPermission.getCode());
        oldEntity.setIcon(authPermission.getIcon());
        oldEntity.setName(authPermission.getName());
        oldEntity.setSort(authPermission.getSort());
        oldEntity.setState(authPermission.getState());
        if (!StringUtils.isEmpty(authPermission.getPath())){
            oldEntity.setPath(authPermission.getPath());
        }
        Map resultMap=new HashMap();
        int add = authPermissionService.update(oldEntity);
        if (add==1){
            resultMap.put("code",200);
        }else{
            resultMap.put("code",500);
        }
        return resultMap;
    }

    /**
     * 修改状态
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/state")
    public Map stateSubmit(HttpServletRequest request,String id){
        Map resultMap=new HashMap();
        AuthPermission ap = authPermissionService.findById(id);
        if (ap.getState().equals(AuthPermissionEnum.STATE_NO.getValue())){
            ap.setState(AuthPermissionEnum.STATE_OK.getValue());
        }else if(ap.getState().equals(AuthPermissionEnum.STATE_OK.getValue())){
            ap.setState(AuthPermissionEnum.STATE_NO.getValue());
        }
        int update = authPermissionService.update(ap);
        if (update==0){
            resultMap.put("code",500);
        }else{
            resultMap.put("code",200);
        }
        return resultMap;
    }

    /**
     * 跳转菜单
     * @param request
     * @param pageNum
     * @param pageSize
     * @param parentId
     * @param state
     * @param name
     * @return
     */
    @GetMapping("/menu/index")
    public String menuIndexHtml(HttpServletRequest request,Integer pageNum, Integer pageSize, String parentId,Integer state, String name){
        Map params=new HashMap();
        params.put("pageNum",pageNum==null?1:pageNum);
        params.put("pageSize",pageSize==null?10:pageSize);
        params.put("parentId",parentId);
        params.put("state",state);
        params.put("name",StringUtils.isEmpty(name)?null:"%"+name+"%");
        PageInfo<AuthPermission> pageInfo = authPermissionService.findPageByParams(params);
        request.setAttribute("pageInfo",pageInfo);

        /**
         * 回显数据
         */
        request.setAttribute("state",state);
        request.setAttribute("name",name);
        request.setAttribute("parentId",parentId);
        return "user/permission/index-menu";
    }

    /**
     * 添加菜单
     * @return
     */
    @GetMapping("/menu/add")
    public String menuaddHtml(HttpServletRequest request,String parentId){
        request.setAttribute("parentId",parentId);
        return "user/permission/add-menu";
    }


    /**
     * 删除数据
     * @param request
     * @param chkids
     * @return
     */
    @ResponseBody
    @PostMapping("/delete")
    public Map deleteSubmit(HttpServletRequest request,String chkids){
        Map resultMap=new HashMap();
        String[] ids = chkids.split(",");
        int delete = authPermissionService.delete(ids);
        if (delete!=ids.length){
            resultMap.put("code",500);
        }else{
            resultMap.put("code",200);
        }
        return resultMap;
    }
}
